package cn.itsub.sodo.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.imageio.ImageIO;


public class FileUtil {
	// filename 文件名 不包含路径  
	// ...args  文件夹名      可以输入多个文件夹名参数  
	public static String getParallelPath(String filename,String ...args)  
	{  
	    String pre=System.getProperty("user.dir");  
	    String path=pre;  
	    for(String arg:args)  
	    {  
	        path+=File.separator+arg;  
	    }  
	    path+=File.separator+filename;  
	    if(path.startsWith("file"))
	    {  
	        path=path.substring(5);  
	    }  
	      
	    return path.replace("/", File.separator);
	}

	public static BufferedImage loadImage(String filename) {
		BufferedImage img = null;
		try {
			InputStream input = FileUtil.class.getClassLoader().getResourceAsStream("assets/" + filename);
			img = ImageIO.read(input);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return img;
	}

	public static InputStream getAssetsAsStream(String path) {
		return FileUtil.class.getClassLoader().getResourceAsStream("assets/" + path);
	}
	public static URL assetsUrl(String path) {
		return FileUtil.class.getClassLoader().getResource("assets/" + path);
	}
	/*
	public static BufferedImage getAssetAsImage(String fileName){
		BufferedImage img = null;
		try {
			String filePath = FileUtil.getParallelPath(fileName, "assets");
			File file = new File(filePath);
			//System.out.println(filePath);
			img = ImageIO.read(file);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return img;
	}
	public static byte[] getAssetAsBytes(String fileName) throws IOException{
		File file = getAssetAsFile(fileName);
		InputStream in = new FileInputStream(file);
		byte[] buffer = new byte[(int) file.length()];
		in.read(buffer);
		return buffer;
	}
	public static File getAssetAsFile(String fileName){
		String real = getParallelPath(fileName, "assets");
		File file = new File(real);
		return new File(real).exists()? file:null;
	}
	*/
	public static void main(String[] args) {
		/*String path = getParallelPath("a4_01.png", "assets");
		System.out.println(path);*/
		InputStream assetsAsStream = getAssetsAsStream("HelloWorld.png");
		System.out.println(assetsAsStream);
	}
}
